window.ROOT = "";
(function() {
	
	var gd={};
	var serverData = {};
	var serverTable;
	jQuery(document).ready(function() {
	serverTable = jQuery('#dataTable').DataTable({
			"serverSide" : true,
			"ordering" : false,
			"ajax" : {
				"dataType" : "json",
				"type" : "get",
				"cache" : false,
				"url" : window.rootPath
						+ '/ui/bussinessAccusation/page',
				"data" : function(d) {
					return $.extend({}, d,
							serverData);
				}
			},
			columns : [
						{
							data : null,
							defaultContent : ""
						},
						{
		                    data: null,
		                    defaultContent: "",
		                    render: function (data, type, full, meta) {
		                    	var operate ='<button type="button" class="btn btn-sm btn-info searchBtn" title="查看"><i class="fa fa-clipboard"></i></button>';
		                    	if(data.infoState!=undefined){
		                    		if(data.infoState.title=='未受理'){
			                    		operate+= '<button type="button" class="btn btn-sm btn-info receiveBtnq" title="快速受理"><i class="fa fa-pencil">受理</i></button>'
			                    	}else if(data.infoState.title=='已受理')
			                    		{
			                    		operate+= '<button type="button" class="btn btn-sm btn-info finishBtnq" title="快速完成"><i class="fa fa-check-circle-o">完成</i></button>'
			                    		}
		                    	}
		                    	operate+='<button type="button" class="btn btn-sm btn-info delBtn" title="删除"><i class="fa fa-remove"></i></button>'
		                        return operate;

		                    }
		                },
		                {
		                	data: null,
		            	    render : function(data,type, full,meta) {
								var dOut;
								if(data.infoState!=undefined){
								if (data.infoState.title == '已受理') {
									dOut = '<td class="center" ><span class="badge label-pill label-primary">已受理</span></td>';
								} else if (data.infoState.title == '未受理') {
									dOut = '<td class="center" ><span class="badge label-pill label-danger">未受理</span></td>';
								} else if (data.infoState.title == '受理完成') {
									dOut = '<td class="center" ><span class="badge label-pill label-success">受理完成</span></td>';
								} else if(data.infoState.title	==null){
									dOut='';
								}
								}
								return dOut;
							}
						},
						{
								data: null,
			            	    render : function(data,type, full,meta) {
									var dOut;
									if(data.resultType!=undefined){
									if (data.resultType.title == '情况不属实') {
										dOut = '<td class="center" ><span class="badge label-pill label-danger">情况不属实</span></td>';
									} else if (data.resultType.title == '情况属实') {
										dOut = '<td class="center" ><span class="badge label-pill label-success">情况属实</span></td>';
									} else if(data.resultType.title	==null){
										dOut='';
									}
									}
									return dOut;
								}
						},
						{
							data : 'rUser.name'
						},
						{
							data : 'businesssellerBaseInfo.shopName'
						},
						{
							data : 'businesssellerBaseInfo.shopAdress'
						},
					
						/*{
							data : 'businesssellerBaseInfo.managementRange'
						},
						
						{
							data : 'businesssellerBaseInfo.managementPerson.name'
						},*/
		                {
		                	data: null,
		                    render:function(data,type,full,meta){
			                	if(data == null || data == undefined || data == ""){
									return data;
								}else{
									return '<div id="'+data.id+'"><script>'
									+'$("#'+data.id+'").raty();$("#'+data.id+'").raty({ number: 5,readOnly: true,size:$("#this").attr("width")-5,score: '+data.accusationScale/20.0+'});'
									+'</script></div>';
								}
							}
		                },
						{
							data : 'accusationType.title'
						},
						{
							data : 'accusationReason',
							render:function(data,type,full,meta){
			                	if(data == null || data == undefined || data == ""){
									return data;
								}else{
									return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
								}
							}
						},
						{
							data : 'accusationDate',
							render: function (data, type, full, meta) {
		                        if (data)
		                            return moment(data).format('YYYY/MM/DD HH:mm');
		                        else
		                            return '';
		                    }
						}],
			"drawCallback" : function(oSettings) {
				var k = 0;
				var total = oSettings.aiDisplay.length
						+ oSettings._iDisplayStart;

				for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
					$('td:eq(0)',oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
					k++;
				}

				serverTable.off('click', 'tr');
				serverTable.on('click','tr',function(event) {
					event = event|| window.event;
					var target = event.target|| event.scrElement;
					var data = serverTable.row(this).data();

					if (target.className.indexOf('fa-pencil') != -1 || target.className.indexOf('receiveBtnq') != -1) {
                        receiveQ(data);
                    }
                    if (target.className.indexOf('fa-check-circle-o') != -1 || target.className.indexOf('finishBtnq') != -1) {
                    	finishQ(data);
                    }
                    
                    if (target.className.indexOf('fa-clipboard') != -1 || target.className.indexOf('searchBtn') != -1) {
                        showDetail(data);
                    }
                    
                    if (target.className.indexOf('fa-remove') != -1 || target.className.indexOf('delBtn') != -1) {
                    	deleteDetail(data);
                    }
                    

					return;
				});
			}
		});
	///完成受理
    jQuery('#finishBtn').on('click', function () {
    	if (jQuery("#finishQForm") && !jQuery("#finishQForm").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {
			return;
		};
		    var id = jQuery('#id').val();
	        var url = window.rootPath + "/ui/bussinessAccusation/finishThing";
	        var d;
	        if(id!=''){
	        	  d = {
	        			"id": id,
	        			"resultTypeId":$("#resultType").val(),
	     	            "memo": jQuery('#memo').val()
	     	        };
	        jQuery.ajax({
	            type: 'post',
	            url: encodeURI(url),
	            data: JSON.stringify(d),
	            dataType: 'json',
	            contentType: 'application/json;charset=utf-8',
	            success: function (data) {
	            	if(data.code==-1){
	            		message("完成失败！");
	            	}
	            	else{
	            		message("受理完成！")
	            	}
	            	jQuery('#dealModal').modal('hide');
	                serverTable.draw(false);
	            }
	        });
	        }
    });
  ///接受受理
    jQuery('#receiveBtn').on('click', function () {
    	if (jQuery("#finishQForm") && !jQuery("#finishQForm").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {
			return;
		};
		if(jQuery('#infoState').val()!="未受理")
			return;
		    var id = jQuery('#id').val();
	        var url = window.rootPath + "/ui/bussinessAccusation/receiveThing";
	        var d;
	        if(id!=''){
	        	  d = {
	        			"id": id
	     	        };
	        jQuery.ajax({
	            type: 'post',
	            url: encodeURI(url),
	            data: JSON.stringify(d),
	            dataType: 'json',
	            contentType: 'application/json;charset=utf-8',
	            success: function (data) {
	            	if(data.code==-1){
	            		message("受理失败！");
	            	}
	            	else{
	            		message("受理成功！")
	            	}
	            	jQuery('#dealModal').modal('hide');
	                serverTable.draw(false);
	            }
	        });
	        }
    });
	var receiveQ=function(data){
        var url = window.rootPath + "/ui/bussinessAccusation/receiveThing";
        var d;
        	  d = {
        			"id": data.id
     	        };
        jQuery.ajax({
            type: 'post',
            url: encodeURI(url),
            data: JSON.stringify(d),
            dataType: 'json',
            contentType: 'application/json;charset=utf-8',
            success: function (data) {
            	if(data.code==-1){
            		message("受理失败！");
            	}
            	else{
            		message("受理成功！")
            	}
                serverTable.draw(false);
            }
        });
    };
    var finishQ=function(data){
    	gateData(data);
    };
   var showDetail=function(data){
	   gateData(data);
   };
   function gateData(data)
   {
	   if(!data) return;
  	    gd=data;
  	    
  	 /* var avatarUrl=window.rootPath+'/a/'+'avatar/'+data.businesssellerBaseInfo.pictureAdress;
  	

		jQuery("#avatar").prop("src",avatarUrl+'?t='+Math.random());*/
	    
  	  var purl=data.businesssellerBaseInfo.pictureAdress;
		if(purl==undefined||purl==""){
			purl="../img/nopicture.png";
		}else{
			purl=window.rootPath+'/'+purl;
		}
		$("#avatar").attr("src",purl);
		
		var path ="";
		if (data.businesssellerBaseInfo.infoState.title == '待认证') {
			path="../img/authentication_in.png";
		} else if (data.businesssellerBaseInfo.infoState.title == '认证拒绝') {
			path="../img/authentication_notaccess.png";
		} else if (data.businesssellerBaseInfo.infoState.title == '认证通过') {
			path="../img/authentication_access.png";
		} else if (data.businesssellerBaseInfo.infoState.title == '初始化') {
			path="../img/authentication_init.png";
		} else if(data.businesssellerBaseInfo.infoState.title	==null){
			path ="";
		}
		$("#infoStatem").attr('src',path); 
		
  	  jQuery('#id').val(data.id);
  	 
		if(data.infoState.title=="已受理"){
		jQuery("#receiveBtn").attr("disabled", true);
		jQuery("#finishBtn").attr("disabled", false);
		jQuery("#resultType").attr("disabled", false);
		jQuery("#memo").attr("disabled", false);
		
	}
	else if(data.infoState.title=="未受理"){
		jQuery("#receiveBtn").attr("disabled", false);
		jQuery("#finishBtn").attr("disabled", true);
		jQuery("#resultType").attr("disabled", true);
		jQuery("#memo").attr("disabled", true);
	}else{
		jQuery("#finishBtn").attr("disabled", true);
		jQuery("#receiveBtn").attr("disabled", true);
		
		jQuery("#resultType").attr("disabled", true);
		jQuery("#memo").attr("disabled", true);
	}
		jQuery('#shopName').val(data.businesssellerBaseInfo.shopName);	
		jQuery('#shopAdress').val(data.businesssellerBaseInfo.shopAdress);
		
		if(data.businesssellerBaseInfo.bussinessLevel!=null){

			jQuery('#areaInfo').val(data.businesssellerBaseInfo.bussinessLevel.levelName);
			jQuery('#areaInfoPhone').val(data.businesssellerBaseInfo.bussinessLevel.phoneNumber);
			if(data.businesssellerBaseInfo.bussinessLevel.manager!=null)
				jQuery('#arreaManagementPerson').val(data.businesssellerBaseInfo.bussinessLevel.manager.name);
		}
		jQuery('#areaMeasure').val(data.businesssellerBaseInfo.areaMeasure);
		jQuery('#signPerson').val(data.businesssellerBaseInfo.signPerson);
		jQuery('#managementRange').val(data.businesssellerBaseInfo.managementRange);
		jQuery('#cleaningPerson').val(data.businesssellerBaseInfo.cleaningPerson);
		if(data.businesssellerBaseInfo.managementPerson!=undefined){
			jQuery('#managementPerson').val(data.businesssellerBaseInfo.managementPerson.name);
		}
		
		jQuery('#infoState').val(data.infoState.title);
		if(data.rUser!=undefined){
			jQuery('#rUser').val(data.rUser.name);
		}
		if(data.accusationType!=undefined)
		{
		jQuery('#accusationType').val(data.accusationType.title);
		}
		if(data.resultType!=undefined)
		{
		jQuery('#resultType').val(data.resultType.id);
		}
		
		jQuery('#memo').val(data.memo);
	jQuery('#dealModal').modal('show');
   };
	//条件检索
	$(".searchBtn").click(function(){
		serverData.key = jQuery("#accusationSearch").val();
		serverTable.draw();
	});
	//撤销查询
    jQuery('.resetBtn').on('click',function(){
        jQuery('#accusationSearch').val('');
        serverData.key='';
        serverTable.draw();
    });
	
function deleteDetail(data){
	if(data==null){
		return;
	}else{
		$.ajax({
			url:rootPath+"/ui/bussinessAccusation/delete/"+data.id,
			data:"_method=DELETE",
			type:'post',
			dataType:'json',
			success:function(result){
				
				if(result.code==1){
					window.location.href=rootPath+"/ui/bussinessAccusation";
				}
			}
		});
	}
};
	

});})();